package mule;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

import javax.swing.*;

/**
 * Class which displays a Player Configuration menu window
 * 
 * @author Ryan Diaz
 * 
 */
public class PlayerConfigWindow extends JFrame {

	private JPanel contentPane;
	private JTextField txtEnterNameHere;
	private int race;
	private String name;
	private Color color;
	private JComboBox comboBox;
	private JButton button;
	private String[] races = { "Flapper", "Alien 1", "Alien 2", "Alien 3",
			"Human" };
	private String[] colors = { "Red", "Blue", "Green", "Yellow" };
	private JComboBox raceBox, colorBox;
	private JLabel raceLabel, nameLabel, colorLabel;

	private JTextField nameText;
	private int level;
	private Player player;
	private static ArrayList<Player> players = new ArrayList<Player>();
	private int count, totalPlayers;
	private Map map;

	/**
	 * Window that holds the PlayerConfigPanel
	 */
	public PlayerConfigWindow(int level, Map map, int totalPlayers, int count) {
		this.level = level;
		this.count = count;
		this.totalPlayers = totalPlayers;
		this.map = map;
		setLayout(new GridLayout(3, 1));
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		setSize(400, 400);
		setVisible(true);
		setLocationRelativeTo(null);
		setResizable(false);
		add(new JLabel("Player " + (count + 1) + " Configuration", null, SwingConstants.CENTER));
		setTitle("M.U.L.E Player Configuration");
		add(new PlayerConfigPanel());
		button = new JButton("Submit");
		button.addActionListener(new action());
		button.setPreferredSize(new Dimension(200, 30));
		add(button);
	}

	/**
	 * return current player
	 * 
	 * @return Player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * return arraylist of all players
	 * 
	 * @return ArrayList<Player>
	 */
	public ArrayList<Player> getPlayers() {
		return players;
	}

	/**
	 * Panel that has the player configuration options on it. Press submit
	 * button to create the player with entered name, race, and color
	 * 
	 */
	class PlayerConfigPanel extends JPanel {

		public PlayerConfigPanel() {
			setLayout(new FlowLayout(FlowLayout.CENTER));

			nameLabel = new JLabel("Enter Name:");
			nameText = new JTextField();
			nameLabel.setPreferredSize(new Dimension(150, 40));
			nameText.setPreferredSize(new Dimension(150, 40));
			add(nameLabel);
			add(nameText);

			raceLabel = new JLabel("Select Race");
			raceBox = new JComboBox(races);
			raceLabel.setPreferredSize(new Dimension(150, 40));
			raceBox.setPreferredSize(new Dimension(150, 40));
			add(raceLabel);
			add(raceBox);

			colorLabel = new JLabel("Select Color");
			colorBox = new JComboBox(colors);
			colorLabel.setPreferredSize(new Dimension(150, 40));
			colorBox.setPreferredSize(new Dimension(150, 40));
			add(colorLabel);
			add(colorBox);

		}

	}

	/**
	 * ActionListener for the button. Creates a player based on the values
	 * given.
	 * 
	 * @author Owner
	 * 
	 */
	class action implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getActionCommand().equals("Submit")) {
				name = nameText.getText();
				if (raceBox.getSelectedItem().equals("Flapper"))
					race = 1;
				else if (raceBox.getSelectedItem().equals("Human"))
					race = 5;
				else if (raceBox.getSelectedItem().equals("Alien 1"))
					race = 2;
				else if (raceBox.getSelectedItem().equals("Alien 2"))
					race = 3;
				else
					race = 4;

				if (colorBox.getSelectedItem().equals("Red"))
					color = Color.RED;
				else if (colorBox.getSelectedItem().equals("Blue"))
					color = Color.BLUE;
				else if (colorBox.getSelectedItem().equals("Green"))
					color = Color.GREEN;
				else
					color = Color.YELLOW;
				dispose();

				player = new Player(level, race, color, name);
				players.add(player);

				if (count + 1 < totalPlayers) {
					new PlayerConfigWindow(level, map, totalPlayers, count + 1);
				}
				if (count == totalPlayers - 1){
					new Game(level, map, totalPlayers, getPlayers());
					MainMenu.clip.stop();
				}
			}
		}

	}
}
